package com.jgx.dao;
import com.jgx.bean.Room502;
import com.jgx.util.Dbutil509;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class RoomDao502 {
    public List<Room502> findAll() {
        Connection connection = Dbutil509.getConnection();
        if (connection == null) {
            System.out.println("数据库连接失败");
            return new ArrayList<>(); // 返回空列表，表示连接失败
        }
        List<Room502> roomList = new ArrayList<>();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String sql = "select * from Room";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Room502 room = new Room502();
                room.setRoomID(resultSet.getInt(1));
                room.setCustomerID(resultSet.getInt(2));
                room.setRoomType(resultSet.getString(3));
                room.setStatus(resultSet.getString(4));
                room.setPrice(resultSet.getString(5));
                room.setTime(resultSet.getString(6));
                roomList.add(room);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                Dbutil509.closeAll(resultSet, statement, connection);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for(Room502 room : roomList)
        System.out.println(room.toString());
        return roomList;
    }

    public void deleteByid(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "delete from room where RoomID = ?";
        try {
            statement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(1, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                Dbutil509.closeAll(null, statement, connection);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void add(Room502 room)  {
        Connection connection = null;
        PreparedStatement statement =null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "INSERT INTO Room(RoomID,CustomerID,RoomType,OccupancyStatus,RoomPrice,ReservationTime) VALUES(?,?,?,?,?,?)";
        try {
            statement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(1,room.getRoomID());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(2, room.getCustomerID());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(3,room.getRoomType());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(4, room.getStatus());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(5, room.getPrice());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(6, room.getTime());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            Dbutil509.closeAll(null, statement, connection);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void update(Room502 room){
        Connection connection = null;
        PreparedStatement statement = null;
        connection = Dbutil509.getConnection();
        System.out.println("创建连接成功");
        String sql = "UPDATE room SET CustomerID=?,RoomType=?,OccupancyStatus=?,RoomPrice=?,ReservationTime=? where RoomID=?";
        try {
            statement=connection.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(6,room.getRoomID());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setInt(1,room.getCustomerID());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(2,room.getRoomType());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(3,room.getStatus());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(4,room.getPrice());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.setString(5,room.getTime());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            Dbutil509.closeAll(null, statement, connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
